package com.antler.smt.portal.service.impl;

import com.antler.smt.constant.CashConstant;
import com.antler.smt.entity.PurchaserCashCouponEntity;
import com.antler.smt.enums.ResultCodeEnum;
import com.antler.smt.portal.mapper.PurchaserCashCouponEntityMapper;
import com.antler.smt.portal.mapper.PurchaserCashCouponInfoMapper;
import com.antler.smt.response.base.Result;
import com.antler.smt.response.portal.GetCashCouponResp;
import com.antler.smt.service.portal.PurchaserCashCouponService;
import com.antler.smt.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 代金券服务实现
 *
 * @author zhangyi
 * @date 2018/5/8 10:14
 */
@Service
public class PurchaserCashCouponServiceImpl implements PurchaserCashCouponService {


    @Autowired
    private PurchaserCashCouponInfoMapper purchaserCashCouponInfoMapper;

    @Autowired
    private PurchaserCashCouponEntityMapper purchaserCashCouponEntityMapper;

    /**
     * 获取代金券列表
     *
     * @param memberId
     * @return
     */
    @Override
    public Result getCashCouponList(Long memberId) {
        Result result = null;

        //采购商代金券列表
        List<GetCashCouponResp> memberCashCouponList =
                purchaserCashCouponInfoMapper.getMemberCashCouponList(memberId);

        for (int i = 0; i < memberCashCouponList.size(); i++){

            memberCashCouponList.get(i).setDesc(getDesc(memberCashCouponList.get(i).getValue()));

            //失效时间
            Date endTime = memberCashCouponList.get(i).getEndTime();
            //创建时间
            Date createTime = memberCashCouponList.get(i).getCreateTime();
            if(endTime.getTime() - createTime.getTime() <= 0){
                //该代金券失效
                memberCashCouponList.get(i).setCashCouponStatus(0);
            }else{
                memberCashCouponList.get(i).setCashCouponStatus(1);
            }

        }
        result = new Result(memberCashCouponList);
        return result;
    }


    /**
     * 废除代金券
     *
     * @param id 代金券id
     * @return
     */
    @Override
    public Result<ResultCodeEnum> abolishCashCoupon(Long id) {
        Result result = null;

        //获取代金券实体
        PurchaserCashCouponEntity purchaserCashCouponEntity1 = purchaserCashCouponEntityMapper.selectByPrimaryKey(id);

        //创建采购商实体
        PurchaserCashCouponEntity purchaserCashCouponEntity = new PurchaserCashCouponEntity();

        //设置作废日期
        purchaserCashCouponEntity.setEndTime(purchaserCashCouponEntity1.getCreateTime());

        purchaserCashCouponEntity.setId(id);

        //设置更新时间
        purchaserCashCouponEntity.setUpdateTime(DateUtil.stampToDate(System.currentTimeMillis()));

        int i = purchaserCashCouponEntityMapper.updateByPrimaryKeySelective(purchaserCashCouponEntity);

        if(0 == i){
            result = new Result(ResultCodeEnum.APPEND_DATA_ERROR);
        }else{
            result = new Result(ResultCodeEnum.OPERATION_SUCCESS);
        }
        return result;
    }

    /**
     * 获取该会员下所有有效的代金券的列表
     *
     * @param memberId
     * @return
     */
    @Override
    public Result getEffectiveCashCouponList(Long memberId) {
        Result result = null;
        List<GetCashCouponResp> effectiveCashCouponList = purchaserCashCouponInfoMapper.getEffectiveCashCouponList(memberId);

        for(int i = 0; i < effectiveCashCouponList.size(); i++){
            effectiveCashCouponList.get(i).setDesc(getDesc(effectiveCashCouponList.get(i).getValue()));
        }

        result = new Result(effectiveCashCouponList);

        return result;
    }

    @Override
    public Result getLoseEfficacyCashCouponList(Long memberId) {

        Result result = null;
        List<GetCashCouponResp> loseEfficacyCashCouponList = purchaserCashCouponInfoMapper.getLoseEfficacyCashCouponList(memberId);

        for(int i = 0; i < loseEfficacyCashCouponList.size(); i++){
            loseEfficacyCashCouponList.get(i).setDesc(getDesc(loseEfficacyCashCouponList.get(i).getValue()));
        }

        result = new Result(loseEfficacyCashCouponList);

        return result;
    }

    /**
     * 根据价格获取描述信息
     *
     * @param value
     * @return
     */
    public String getDesc(int value){

            if(value == CashConstant.PRICE_20){
                return CashConstant.PRICE_20_DESC;
            }else if(value == CashConstant.PRICE_80){
                return CashConstant.PRICE_80_DESC;
            }else if (value == CashConstant.PRICE_200){
                return CashConstant.PRICE_200_DESC;
            }else if(value == CashConstant.PRICE_300){
                return CashConstant.PRICE_300_DESC;
            }else if(value == CashConstant.PRICE_500){
                return CashConstant.PRICE_500_DESC;
            }else if(value == CashConstant.PRICE_1000){
                return CashConstant.PRICE_1000_DESC;
            }else if(value == CashConstant.PRICE_2000){
                return CashConstant.PRICE_2000_DESC;
            }
            return null;
    }
}
